package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"www/test/iploc"
)

type IplocResp struct {
	Ip   string
	Addr string
}

func main() {
	r := gin.Default()
	r.GET("/toCity/:ip", func(c *gin.Context) {
		loc, err := iploc.Open("./qqwry1.dat")

		if err != nil {
			fmt.Println(err)
			return
		}
		ip := c.Params.ByName("ip")
		detail := loc.Find(ip)

		//fmt.Println(detail.Country, detail.Province, detail.City, detail.County)
		var addr string
		if detail.Region != "" {
			addr += "[" + detail.Region + "]-"
		}
		if detail.Country != "" {
			addr += detail.Country + "-"
		}
		if detail.Province != "" {
			addr += detail.Province + "-"
		}
		if detail.City != "" {
			addr += detail.City + "-"
		}
		//log.Errorf("Shi ma ", detail)
		res := &IplocResp{
			Ip:   ip,
			Addr: addr,
		}
		c.JSON(http.StatusOK, res)
	})
	r.Run(":8889")
}
